package com.ndp.fb.walle.controller;

import com.alibaba.fastjson.JSON;
import com.ndp.fb.enums.ErrorType;
import com.ndp.fb.rdb.model.User;
import com.ndp.fb.walle.business.RuleSettingsService;
import com.ndp.fb.walle.model.vo.OutputResult;
import com.ndp.fb.walle.model.vo.rulesetting.param.SettingsVo;
import com.ndp.fb.walle.util.ResponseUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户优化设定界面接口
 * Created by hunk.qin on 2015/9/14.
 */
@RequestMapping("/ruleSettings")
@Controller
public class RuleSettingsController {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RuleSettingsService ruleSettingsService;

    /**
     * 规则设置列表
     * @param session
     * @return
     */
    @RequestMapping(value = "list")
    @ResponseBody
    public ResponseEntity<String> settingsList(HttpSession session) {
        logger.info("setting list  begin.");
        ErrorType errorType = null;
        Object data = null;
        try {
            User user = (User) session.getAttribute("user");
            data=ruleSettingsService.getSettingsVoByUserId(user.getTicket(), user.getOrgId());
        } catch (Exception e) {
            errorType = ErrorType.INTERNAL_EXCEPTION;
            logger.error(e.getMessage(), e);
        }
        OutputResult outputResult = ResponseUtils.bulidOutputResult(errorType, data);
        logger.info("setting  list end.");
        return ResponseUtils.returnJsonWithUTF8(JSON.toJSONString(outputResult));
    }

    /**
     * 保存规则设置
     * @param param
     * @param session
     * @return
     */
    @RequestMapping(value = "save",method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> settingsSave(@RequestParam(value = "param", required = true) String param,HttpSession session) {
        logger.info("setting save  begin.");
        ErrorType errorType = null;
        Object data = null;
        try {
            User user = (User) session.getAttribute("user");
            List<SettingsVo> settingsVoList=JSON.parseArray(param,SettingsVo.class);
            ruleSettingsService.saveSettings(settingsVoList,user.getTicket());
            data="ok";
        } catch (Exception e) {
            errorType = ErrorType.INTERNAL_EXCEPTION;
            logger.error(e.getMessage(), e);
        }
        OutputResult outputResult = ResponseUtils.bulidOutputResult(errorType, data);
        logger.info("setting  save end.");
        return ResponseUtils.returnJsonWithUTF8(JSON.toJSONString(outputResult));
    }
}
